package com.fhz.library_log.logger

import android.content.Context
import com.fhz.library_log.ILogger
import com.fhz.library_log.LoggerLevel
import java.io.File
import java.io.FileWriter
import java.text.SimpleDateFormat
import java.util.*

/**
 * 时间:2023/10/17
 * @author Mr.Feng
 * 简述: TODO
 */
class FileLogger : ILogger {

    private var isDebug = true //是否Debug
    private var TAG = "" //默认TAG
    private var level = LoggerLevel.DEBUG // 默认日志等级
    private var url = "log" //Log存储位置，可以是本地路径 可以是网络路径 可以是Email地址
    private var context:Context? = null //上下文 目的是获得内部存储的路径
    private var loggerFormat = "(%s --->%s）" //格式化


    private fun getFile(): File {
        context.let {
            val filesDir = it?.filesDir //data/data/files
            return File(filesDir, url) //new File('data/data/file/log)
        }
    }
    private fun getTime():String{
        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
        val date = Date()
        return simpleDateFormat.format(date)
    }
    override fun d(tag: String, log: String) {
        if(isDebug && level.ordinal >= LoggerLevel.DEBUG.ordinal){
            val time = getTime()
            val message = String.format(loggerFormat, tag, log)

            FileWriter(getFile(),true).apply {//true代表追加数据
                write("$time D/$TAG: $message")
                write("\n")
                flush()
                close()
            }
        }
    }

    override fun v(tag: String, log: String) {
        if(isDebug && level.ordinal >= LoggerLevel.VREBOSE.ordinal){
            val time = getTime()
            val message = String.format(loggerFormat, tag, log)
            FileWriter(getFile(),true).apply {
                write("$time V/$TAG: $message")
                write("\n")
                flush()
                close()
            }
        }
    }

    override fun i(tag: String, log: String) {
        if(isDebug && level.ordinal >= LoggerLevel.INFO.ordinal){
            val time = getTime()
            val message = String.format(loggerFormat, tag, log)
            FileWriter(getFile(),true).apply {
                write("$time I/$TAG: $message")
                write("\n")
                flush()
                close()
            }
        }
    }

    override fun w(tag: String, log: String) {
        if(isDebug && level.ordinal >= LoggerLevel.WARN.ordinal){
            val time = getTime()
            val message = String.format(loggerFormat, tag, log)
            FileWriter(getFile(),true).apply {
                write("$time W/$TAG: $message")
                write("\n")
                flush()
                close()
            }
        }
    }

    override fun e(tag: String, log: String) {
        if(isDebug && level.ordinal >= LoggerLevel.ERROR.ordinal){
            val time = getTime()
            val message = String.format(loggerFormat, tag, log)
            FileWriter(getFile(),true).apply {
                write("$time E/$TAG: $message")
                write("\n")
                flush()
                close()
            }
        }
    }

    override fun setDebug(isDeug: Boolean) {
        this.isDebug = isDebug
    }

    override fun setLogTAG(TAG: String) {
        this.TAG = TAG
    }

    override fun setLoggerLevel(level: LoggerLevel) {
        this.level = level
    }

    override fun setSaveUrl(url: String) {
        this.url = url
    }

    //上下文
    override fun setContext(context: Context?) {
        this.context = context
    }
}